package com.fhz.mvitest.utils

import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 时间:2023/5/19
 * @author Mr.Feng
 * 简述: 网络请求工具类
 */
object RetrofitFactory  {
    private val retrofit: Retrofit

    //http://43.143.146.165:7777/banner/json
    init {
        retrofit=Retrofit.Builder()
            .baseUrl("http://43.143.146.165:7777/")
            .addConverterFactory(GsonConverterFactory.create())
//            .addCallAdapterFactory(CoroutineCallAdapterFactory.create)
            .client(createOkHttpClient())
            .build()
    }

    private fun createOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
//            .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
//            .readTimeout(TIMEOUT, TimeUnit.SECONDS)
//            .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
//            .addNetworkInterceptor(createLoggingInterceptor())
            .addInterceptor(createLoggingInterceptor())
//            .addInterceptor(createTokenInterceptor())
//            .addInterceptor(createNormalErrorHttpCodeInterceptor())
            .build()
    }

    private fun createLoggingInterceptor(): Interceptor {
        return HttpLoggingInterceptor().apply { level=HttpLoggingInterceptor.Level.BODY }
    }


    /**
     * create api instance
     */
    fun <T> create(clazz: Class<T>) : T =  retrofit.create(clazz)


}